package com.chart.web.controller.chart.sta;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.domian.dto.ChartDateTto;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.core.controller.BaseController;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ExcelDataUtils;
import com.chart.common.utils.ExcelUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 3D折线图
 *
 * @author chart
 */
@RestController
@RequestMapping("/rowLine3D/chart")
public class RowLine3DChartController extends BaseController {


    @Autowired
    private IChartConfigsService chartConfigsService;


    //3D折线图
    @PostMapping("/multi3DLine/importData")
    public AjaxResult importData(MultipartFile file, Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> minList = getdata.get(0);
            List<String> maxList = getdata.get(1);

            String minV = minList.get(1);
            String maxV = maxList.get(1);

            List<String> xList = new ArrayList<>();
            List<String> yList = new ArrayList<>();


            List<List<String>> dataList = new ArrayList<>();
            for (int i = 2; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                if (!CollectionUtils.isEmpty(list)) {
                    List<String> newList = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        String s = list.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            newList.add(s);
                        }
                    }
                    if (!CollectionUtils.isEmpty(newList)) {
                        if (i == 2) {
                            newList.remove(0);
                            yList.addAll(newList);
                        } else {
                            String x = newList.get(0);
                            if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                                xList.add(x);
                            }
                            newList.remove(0);
                            dataList.add(newList);
                        }
                    }
                }
            }


            List<List<Object>> newDataList = new ArrayList<>();
            for (int i = 0; i < yList.size(); i++) {
                String y = yList.get(i);
                for (int j = 0; j < xList.size(); j++) {
                    String x = xList.get(j);
                    List<Object> cellList = new ArrayList<>();
                    cellList.add(x);
                    cellList.add(y);
                    cellList.add(Double.parseDouble(dataList.get(j).get(i)));
                    newDataList.add(cellList);
                }
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(0);
            jsonObject1.put("data", newDataList);
            series.remove(0);
            series.add(jsonObject1);
            jsonObject.put("series", series);

            JSONObject minMaxAxis = (JSONObject) jsonObject.get("visualMap");
            minMaxAxis.put("min", Double.parseDouble(minV));
            minMaxAxis.put("max", Double.parseDouble(maxV));
            jsonObject.put("visualMap", minMaxAxis);

            JSONObject grid3D = (JSONObject) jsonObject.get("grid3D");
            if (xList.size() > 10 || yList.size() > 10) {
                grid3D.put("boxWidth", xList.size() * 8);
                grid3D.put("boxDepth", yList.size() * 4);

            } else if (xList.size() <= 10 && yList.size() <= 10) {
                grid3D.put("boxWidth", xList.size() * 20);
                grid3D.put("boxDepth", yList.size() * 10);
            }
            jsonObject.put("grid3D", grid3D);


            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
            workbook.close();
        }
    }


    //3D折线图
    @PostMapping("/multi3DLine/v2/importData")
    public AjaxResult v2importData(@RequestBody List<Map<String, Object>> dataLists
            , Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
     /*       workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);*/

            Sheet sheet = ExcelUtils.convertDataListToSheetRemoveNineAndOne(dataLists);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> minList = getdata.get(0);
            List<String> maxList = getdata.get(1);

            String minV = minList.get(1);
            String maxV = maxList.get(1);

            List<String> xList = new ArrayList<>();
            List<String> yList = new ArrayList<>();


            List<List<String>> dataList = new ArrayList<>();
            for (int i = 2; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                if (!CollectionUtils.isEmpty(list)) {
                    List<String> newList = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        String s = list.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            newList.add(s);
                        }
                    }
                    if (!CollectionUtils.isEmpty(newList)) {
                        if (i == 2) {
                            newList.remove(0);
                            yList.addAll(newList);
                        } else {
                            String x = newList.get(0);
                            if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                                xList.add(x);
                            }
                            newList.remove(0);
                            dataList.add(newList);
                        }
                    }
                }
            }


            List<List<Object>> newDataList = new ArrayList<>();
            for (int i = 0; i < yList.size(); i++) {
                String y = yList.get(i);
                for (int j = 0; j < xList.size(); j++) {
                    String x = xList.get(j);
                    List<Object> cellList = new ArrayList<>();
                    cellList.add(x);
                    cellList.add(y);
                    cellList.add(Double.parseDouble(dataList.get(j).get(i)));
                    newDataList.add(cellList);
                }
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(0);
            jsonObject1.put("data", newDataList);
            series.remove(0);
            series.add(jsonObject1);
            jsonObject.put("series", series);

            JSONObject minMaxAxis = (JSONObject) jsonObject.get("visualMap");
            minMaxAxis.put("min", Double.parseDouble(minV));
            minMaxAxis.put("max", Double.parseDouble(maxV));
            jsonObject.put("visualMap", minMaxAxis);

            JSONObject grid3D = (JSONObject) jsonObject.get("grid3D");
            if (xList.size() > 10 || yList.size() > 10) {
                grid3D.put("boxWidth", xList.size() * 8);
                grid3D.put("boxDepth", yList.size() * 4);

            } else if (xList.size() <= 10 && yList.size() <= 10) {
                grid3D.put("boxWidth", xList.size() * 20);
                grid3D.put("boxDepth", yList.size() * 10);
            }
            jsonObject.put("grid3D", grid3D);


            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
            workbook.close();
        }
    }

    //3D折线切面图
    @PostMapping("/multi3DSectonLine/importData")
    public AjaxResult v2multi3DSectonLines(@RequestBody ChartDateTto chartDateTto) throws Exception {
        //    XSSFWorkbook workbook = null;
        try {
 /*           workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);*/

            Sheet sheet = ExcelUtils.convertDataListToSheetRemoveNineAndOne(chartDateTto.getDataList());
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

//            List<String> minList = getdata.get(0);
//            List<String> maxList = getdata.get(1);
            List<String> pdata = getdata.get(2);
            List<Object> pList = new ArrayList<>();
            for (int i = 1; i < pdata.size(); i++) {
                String s = pdata.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    pList.add(Double.parseDouble(s));
                }
            }

//            String minV = minList.get(1);
//            String maxV = maxList.get(1);


            List<String> xList = new ArrayList<>();
            List<String> yList = new ArrayList<>();


            List<List<String>> dataList = new ArrayList<>();
            for (int i = 3; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                if (!CollectionUtils.isEmpty(list)) {
                    List<String> newList = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        String s = list.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            newList.add(s);
                        }
                    }
                    if (!CollectionUtils.isEmpty(newList)) {
                        if (i == 3) {
                            newList.remove(0);
                            yList.addAll(newList);
                        } else {
                            String x = newList.get(0);
                            if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                                xList.add(x);
                            }
                            newList.remove(0);
                            dataList.add(newList);
                        }
                    }
                }
            }


            List<List<Object>> newDataLists = new ArrayList<>();
            List<List<Object>> newDataList = new ArrayList<>();
            for (int i = 0; i < yList.size(); i++) {
                String y = yList.get(i);
                for (int j = 0; j < xList.size(); j++) {
                    String x = xList.get(j);
                    List<Object> cellList = new ArrayList<>();
                    cellList.add(x);
                    cellList.add(y);
                    cellList.add(Double.parseDouble(dataList.get(j).get(i)));
                    newDataList.add(cellList);

                    List<Object> cellList1 = new ArrayList<>();
                    cellList1.add(x);
                    cellList1.add(y);
                    cellList1.add(Double.parseDouble(dataList.get(j).get(i)));
                    newDataLists.add(cellList1);
                }
            }


            //ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(chartDateTto.getChartValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(0);
            JSONObject jsonObject2 = series.get(1);
            List<JSONObject> newList = new ArrayList<>();

            jsonObject1.put("data", newDataList);
            newList.add(jsonObject1);

            for (int i = 0; i < pList.size(); i++) {
                JSONObject jsonObject3 = new JSONObject();
                jsonObject3.putAll(jsonObject2);
                jsonObject3.put("id", i + 2);
                List<List<Object>> list = new ArrayList<>();
                for (int i1 = 0; i1 < newDataLists.size(); i1++) {
                    List<Object> objects = newDataLists.get(i1);
                    List<Object> objects1 = new ArrayList<>();
                    objects1.addAll(objects);
                    objects1.set(2, pList.get(i));
                    list.add(objects1);
                }
                jsonObject3.put("data", list);
                newList.add(jsonObject3);
            }


            jsonObject.put("series", newList);

//            JSONObject minMaxAxis = (JSONObject) jsonObject.get("visualMap");
//            minMaxAxis.put("min", Double.parseDouble(minV));
//            minMaxAxis.put("max", Double.parseDouble(maxV));
//            jsonObject.put("visualMap", minMaxAxis);

/*            JSONObject grid3D = (JSONObject) jsonObject.get("grid3D");
            if (xList.size() > 10 || yList.size() > 10) {
                grid3D.put("boxWidth", xList.size() * 8);
                grid3D.put("boxDepth", yList.size() * 4);

            } else if (xList.size() <= 10 && yList.size() <= 10) {
                grid3D.put("boxWidth", xList.size() * 20);
                grid3D.put("boxDepth", yList.size() * 10);
            }
            jsonObject.put("grid3D", grid3D);*/


            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
//            workbook.close();
        }
    }

    //3D折线切面图
    //@PostMapping("/multi3DSectonLine/importData")
    public AjaxResult multi3DSectonLine(MultipartFile file, Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

//            List<String> minList = getdata.get(0);
//            List<String> maxList = getdata.get(1);
            List<String> pdata = getdata.get(2);
            List<Object> pList = new ArrayList<>();
            for (int i = 1; i < pdata.size(); i++) {
                String s = pdata.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    pList.add(Double.parseDouble(s));
                }
            }

//            String minV = minList.get(1);
//            String maxV = maxList.get(1);


            List<String> xList = new ArrayList<>();
            List<String> yList = new ArrayList<>();


            List<List<String>> dataList = new ArrayList<>();
            for (int i = 3; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                if (!CollectionUtils.isEmpty(list)) {
                    List<String> newList = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        String s = list.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            newList.add(s);
                        }
                    }
                    if (!CollectionUtils.isEmpty(newList)) {
                        if (i == 3) {
                            newList.remove(0);
                            yList.addAll(newList);
                        } else {
                            String x = newList.get(0);
                            if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                                xList.add(x);
                            }
                            newList.remove(0);
                            dataList.add(newList);
                        }
                    }
                }
            }


            List<List<Object>> newDataLists = new ArrayList<>();
            List<List<Object>> newDataList = new ArrayList<>();
            for (int i = 0; i < yList.size(); i++) {
                String y = yList.get(i);
                for (int j = 0; j < xList.size(); j++) {
                    String x = xList.get(j);
                    List<Object> cellList = new ArrayList<>();
                    cellList.add(x);
                    cellList.add(y);
                    cellList.add(Double.parseDouble(dataList.get(j).get(i)));
                    newDataList.add(cellList);

                    List<Object> cellList1 = new ArrayList<>();
                    cellList1.add(x);
                    cellList1.add(y);
                    cellList1.add(Double.parseDouble(dataList.get(j).get(i)));
                    newDataLists.add(cellList1);
                }
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(0);
            JSONObject jsonObject2 = series.get(1);
            List<JSONObject> newList = new ArrayList<>();

            jsonObject1.put("data", newDataList);
            newList.add(jsonObject1);

            for (int i = 0; i < pList.size(); i++) {
                JSONObject jsonObject3 = new JSONObject();
                jsonObject3.putAll(jsonObject2);
                jsonObject3.put("id", i + 2);
                List<List<Object>> list = new ArrayList<>();
                for (int i1 = 0; i1 < newDataLists.size(); i1++) {
                    List<Object> objects = newDataLists.get(i1);
                    List<Object> objects1 = new ArrayList<>();
                    objects1.addAll(objects);
                    objects1.set(2, pList.get(i));
                    list.add(objects1);
                }
                jsonObject3.put("data", list);
                newList.add(jsonObject3);
            }


            jsonObject.put("series", newList);

//            JSONObject minMaxAxis = (JSONObject) jsonObject.get("visualMap");
//            minMaxAxis.put("min", Double.parseDouble(minV));
//            minMaxAxis.put("max", Double.parseDouble(maxV));
//            jsonObject.put("visualMap", minMaxAxis);

            JSONObject grid3D = (JSONObject) jsonObject.get("grid3D");
            if (xList.size() > 10 || yList.size() > 10) {
                grid3D.put("boxWidth", xList.size() * 8);
                grid3D.put("boxDepth", yList.size() * 4);

            } else if (xList.size() <= 10 && yList.size() <= 10) {
                grid3D.put("boxWidth", xList.size() * 20);
                grid3D.put("boxDepth", yList.size() * 10);
            }
            jsonObject.put("grid3D", grid3D);


            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
            workbook.close();
        }
    }

    //3D折线切面图
    @PostMapping("/multi3DSectonLine/v2/importData")
    public AjaxResult v2multi3DSectonLine(@RequestBody List<Map<String, Object>> dataLists
            , Long configId) throws Exception {
        //    XSSFWorkbook workbook = null;
        try {
 /*           workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);*/

            Sheet sheet = ExcelUtils.convertDataListToSheetRemoveNineAndOne(dataLists);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

//            List<String> minList = getdata.get(0);
//            List<String> maxList = getdata.get(1);
            List<String> pdata = getdata.get(2);
            List<Object> pList = new ArrayList<>();
            for (int i = 1; i < pdata.size(); i++) {
                String s = pdata.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    pList.add(Double.parseDouble(s));
                }
            }

//            String minV = minList.get(1);
//            String maxV = maxList.get(1);


            List<String> xList = new ArrayList<>();
            List<String> yList = new ArrayList<>();


            List<List<String>> dataList = new ArrayList<>();
            for (int i = 3; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                if (!CollectionUtils.isEmpty(list)) {
                    List<String> newList = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        String s = list.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            newList.add(s);
                        }
                    }
                    if (!CollectionUtils.isEmpty(newList)) {
                        if (i == 3) {
                            newList.remove(0);
                            yList.addAll(newList);
                        } else {
                            String x = newList.get(0);
                            if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                                xList.add(x);
                            }
                            newList.remove(0);
                            dataList.add(newList);
                        }
                    }
                }
            }


            List<List<Object>> newDataLists = new ArrayList<>();
            List<List<Object>> newDataList = new ArrayList<>();
            for (int i = 0; i < yList.size(); i++) {
                String y = yList.get(i);
                for (int j = 0; j < xList.size(); j++) {
                    String x = xList.get(j);
                    List<Object> cellList = new ArrayList<>();
                    cellList.add(x);
                    cellList.add(y);
                    cellList.add(Double.parseDouble(dataList.get(j).get(i)));
                    newDataList.add(cellList);

                    List<Object> cellList1 = new ArrayList<>();
                    cellList1.add(x);
                    cellList1.add(y);
                    cellList1.add(Double.parseDouble(dataList.get(j).get(i)));
                    newDataLists.add(cellList1);
                }
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(0);
            JSONObject jsonObject2 = series.get(1);
            List<JSONObject> newList = new ArrayList<>();

            jsonObject1.put("data", newDataList);
            newList.add(jsonObject1);

            for (int i = 0; i < pList.size(); i++) {
                JSONObject jsonObject3 = new JSONObject();
                jsonObject3.putAll(jsonObject2);
                jsonObject3.put("id", i + 2);
                List<List<Object>> list = new ArrayList<>();
                for (int i1 = 0; i1 < newDataLists.size(); i1++) {
                    List<Object> objects = newDataLists.get(i1);
                    List<Object> objects1 = new ArrayList<>();
                    objects1.addAll(objects);
                    objects1.set(2, pList.get(i));
                    list.add(objects1);
                }
                jsonObject3.put("data", list);
                newList.add(jsonObject3);
            }


            jsonObject.put("series", newList);

//            JSONObject minMaxAxis = (JSONObject) jsonObject.get("visualMap");
//            minMaxAxis.put("min", Double.parseDouble(minV));
//            minMaxAxis.put("max", Double.parseDouble(maxV));
//            jsonObject.put("visualMap", minMaxAxis);

            JSONObject grid3D = (JSONObject) jsonObject.get("grid3D");
            if (xList.size() > 10 || yList.size() > 10) {
                grid3D.put("boxWidth", xList.size() * 8);
                grid3D.put("boxDepth", yList.size() * 4);

            } else if (xList.size() <= 10 && yList.size() <= 10) {
                grid3D.put("boxWidth", xList.size() * 20);
                grid3D.put("boxDepth", yList.size() * 10);
            }
            jsonObject.put("grid3D", grid3D);


            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
//            workbook.close();
        }
    }
}
